#pragma once
#include<deque>
#include<random>
#include<Windows.h>
#include "Basket.h"
#include"ObjectList.h"
#include"Poultry.h"
#include"Hen.h"
#include"Duck.h"
#include"Goose.h"
#include"BackGround.h"
using namespace std;

class _Game : protected Background
{
	private:
		deque <Poultry*> poultry;//Danh sach cac gia cam
		Background background; //background cua game
		Basket basket;//Gio hung trung
		public:
			int levelTmp;//Luu tru muc do level cu
	   //Ham hien thi cac texture trong game
		void display(){
			if (!bGame)
				return;

				glClear(GL_COLOR_BUFFER_BIT);
		
				//Kiem tra xem gam la moi bat dau hay ket thu
				if((bGameStart==false)&&(bGameOver==false)){
					background.display();
					//Duyt danh sach gia cam hien thi 
					for(int i=0;i< (int)poultry.size();i++)
					{
						//Kiem tra loai gia cam
						if(poultry[i]->strNameClass=="DUCK"){
							(((Duck*)poultry[i])->objectList).display();
							(((Duck*)poultry[i])->objectList).timer();
							((Duck*)poultry[i])->display();
							((Duck*)poultry[i])->timer();
						}else if(poultry[i]->strNameClass=="HEN"){
								(((Hen*)poultry[i])->objectList).display();
								(((Hen*)poultry[i])->objectList).timer();
								((Hen*)poultry[i])->display();
								((Hen*)poultry[i])->timer();
							}else{
								(((Goose*)poultry[i])->objectList).display();
								(((Goose*)poultry[i])->objectList).timer();
								((Goose*)poultry[i])->display();
								((Goose*)poultry[i])->timer();
							}
					}

					basket.display();//Hien thi gio hung
					Mouse.display();//Hien thi chuot
					//Ve cac thong so len man hinh nhu level, score
					glColor(0, 0, 0);
					char Text[100];
					char Temp[100];
					//Display level on screen
					strcpy(Text, "Level: ");
					strcat(Text, _itoa(iLevel, Temp, 10));
					glText(-WIDTH / 2 +20 ,-HEIGHT / 2 + 25, Text,8);
					//Display score on screen
					glColor(0, 0, 0);
					strcpy(Text, "Score: ");
					strcat(Text, _itoa(iScore, Temp, 10));
					glText(-WIDTH / 2 +120, -HEIGHT / 2 + 25, Text,8);
					//Hien thi so trung roi ra ngoai ro
					glPushMatrix();
					glColor3f(1, 1, 1);
					glTranslatef(-WIDTH / 2 + 240, - HEIGHT / 2 + 32, 0);
					glTranslatef(-(-WIDTH / 2 + 240), -(- HEIGHT / 2 + 32),0);
					DrawTexture(-WIDTH / 2 + 240,- HEIGHT / 2 + 32, EGG, Textures[EGG].width/6 , Textures[EGG].height/6);
					glColor(0, 0, 0);
					strcpy(Text, "");
					strcat(Text, _itoa(iBrokenEggs, Temp, 10));
					glText(-WIDTH / 2 + 255,- HEIGHT / 2 + 25, Text,8); 
					//Hien thi so shit hung nham
					glPushMatrix();
					glColor3f(1, 1, 1);
					glTranslatef(-WIDTH / 2 + 300, - HEIGHT / 2 + 32, 0);
					glTranslatef(-(-WIDTH / 2 + 300), -(- HEIGHT / 2 + 32),0);
					DrawTexture(-WIDTH / 2 + 300,- HEIGHT / 2 + 32, SHIT, Textures[SHIT].width/6 , Textures[SHIT].height/6);
					glPopMatrix();
					glColor(0, 0, 0);
					strcpy(Text, "");
					strcat(Text, _itoa(iShit, Temp, 10));
					glText(-WIDTH / 2 + 315,- HEIGHT / 2 + 25, Text,8); 

					strcpy(Text, "ESC : Exit ");
					glText(-WIDTH / 2 + 700,- HEIGHT / 2 + 25, Text,8); 
				}else{
					if(bGameStart){//Neu game la moi bat dau
						char Text[100];
						glColor(0, 0, 0);
						strcpy(Text, "USE MOUSE OR KEYBOARD (LEFT, RIGHT) TO MOVE BASKET TO CATCH EGGS ");
						glText( -WIDTH/2+50,HEIGHT/2-100, Text,8);
						strcpy(Text, "IF EGGS FALL OUT OF BASKET 3 TIMES, YOU WILL END THE GAME");
						glText( -WIDTH/2+50,HEIGHT/2-140, Text,8);
						strcpy(Text, "IF YOU CATCH SHIT THREE TIME , YOU WILL END THE GAME ");
						glText( -WIDTH/2+50,HEIGHT/2-180, Text,8);
						strcpy(Text, "THE LEVEL IS INCREASED AFTER 20 EGGS");
						glText( -WIDTH/2+50,HEIGHT/2-220, Text,8);
						strcpy(Text, "PRESS N TO ADD A HEN ON THE FENCE");
						glText( -WIDTH/2+50,HEIGHT/2-260, Text,8);
						strcpy(Text, "PRESS G TO ADD A GOOSE ON THE FENCE");
						glText( -WIDTH/2+50,HEIGHT/2-300, Text,8);
						strcpy(Text, "PRESS V TO ADD A DUCK ON THE FENCE");
						glText( -WIDTH/2+50,HEIGHT/2-340, Text,8);
						strcpy(Text, "PRESS ESC TO EXIT");
						glText( -WIDTH/2+50,HEIGHT/2-380, Text,8);
						strcpy(Text, "PRESS P TO PAUSE OR  CONTINUE");
						glText( -WIDTH/2+50,HEIGHT/2-420, Text,8);
						strcpy(Text, "PRESS ENTER TO START ");
						glText(-WIDTH/2+50, HEIGHT/2-500, Text,8);

					}else{//Neu gam ket thuc
						char Text[100];
						char Temp[100];
						glColor(0, 0, 0);
						strcpy(Text, "GAME OVER PRESS ENTER TO START ");
						glText(0 -150 ,0 , Text,8);
						strcpy(Text, "Your score: ");
						strcat(Text, _itoa(iScore, Temp, 10));
						glText(0 -150, 0-50, Text,8);
					}
				}
				glutSwapBuffers();
				glEnd();
		}

		//Timer cho game
		void Timer()
		{
			if (!bGame)
				return;
			//Kiem tra neu so trung roi ra ngoai toi da
			//hay hung so shit toi da thi ket thuc game
			if(iBrokenEggs==BEGG||iShit==CSHIT){
				gameOver();
			}
			
			//Kiem tra level 
			iLevel=iScore/20+1;
			if(levelTmp<iLevel){
				levelTmp = iLevel;
				fSpeed=fSpeed+0.2;
			}
			//Kiem tra thoi diem gia cam san sinh cu moi 5 giay			
			const float t = float(glutGet(GLUT_ELAPSED_TIME));
			for(int i=0;i< (int)poultry.size();i++)
			{
				
					if(poultry[i]->strNameClass=="DUCK" && ((t-((Duck*)poultry[i])->t0))>=TIME){

							((Duck*)poultry[i])->objectList.produce(((Duck*)poultry[i])->x,((Duck*)poultry[i])->y-80,rand()%(1-0+1)+0 );
					
					((Duck*)poultry[i])->t0=t;
					}
					else
						if(poultry[i]->strNameClass=="HEN" && ((t-((Hen*)poultry[i])->t0))>=TIME){

								((Hen*)poultry[i])->objectList.produce(((Hen*)poultry[i])->x,((Hen*)poultry[i])->y-80,rand()%(1-0+1)+0 );
					
						((Hen*)poultry[i])->t0=t;
						}
						else
							if(poultry[i]->strNameClass=="GOOSE" && ((t-((Goose*)poultry[i])->t0))>=TIME){

									((Goose*)poultry[i])->objectList.produce(((Goose*)poultry[i])->x,((Goose*)poultry[i])->y-80,rand()%(1-0+1)+0 );
					
							((Goose*)poultry[i])->t0=t;
							}
			}

			
			basket.Timer();
		}
			void start(){
				bGame=true;
				bGameOver = false;
				bGameStart=true;
			}
		void newGame(){
			bGame = true;
			bGameOver=false;
			bGameStart=false;
			glClear(GL_COLOR_BUFFER_BIT);
			poultry.clear();
			glutSwapBuffers();
			PlaySound(TEXT("Sound/music.wav"), NULL, SND_ASYNC | SND_LOOP);
			levelTmp =1;
			iScore =0;
			iLevel =1;
			iBrokenEggs=0;
			iShit=0;
			fSpeed=0.5;
			poultry.push_back(new Hen());
		}
		void gameOver(){
			poultry.clear();
			bGame =true;
			bGameOver=true;
			bGameStart=false;
	
		}

		void pause(){
		bGame =false;
		}
		void resume(){
			bGame =true;
		}
		void Keyboard(int key)
		{
			if (!bGame)
				return;

			basket.Keyboard(key);
		}

		void Keyboard(unsigned char button)
		{
		
			switch (button)
			{
				case 'g': case 'G':
					poultry.push_back(new Hen());
					break;

				case 'n': case 'N':
					poultry.push_back(new Goose());
					break;

				case 'v': case 'V':
						poultry.push_back(new Duck());
						break;
				case 'p': case 'P':
				{
					if(!bPause){
						pause();
						bPause = true;
					}else{ resume();bPause = false;}
				}
				break;
				case 13:
					newGame();
				break;

				case 27:
					exit(0);
					break;
				default:
					break;
			}
		}
} Game;

